use anyhow::Ok;
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};

use crate::{entity, model::Bucket};

pub async fn find_by_domain(
    db: &DatabaseConnection,
    domain: &str,
) -> anyhow::Result<Option<Bucket>> {
    let bucket = entity::bucket::Entity::find()
        .filter(entity::bucket::Column::Domain.eq(domain))
        .one(db)
        .await?
        .map(|entity| Bucket::from(entity));

    Ok(bucket)
}

pub async fn find_by_name(db: &DatabaseConnection, name: &str) -> anyhow::Result<Option<Bucket>> {
    let bucket = entity::bucket::Entity::find()
        .filter(entity::bucket::Column::Name.eq(name))
        .one(db)
        .await?
        .map(|entity| Bucket::from(entity));

    Ok(bucket)
}
